package com.xmut.DengLu;

import com.Model.Pojo.Users;
import com.sql.DAO;

import com.xmut.ZhuCaiDan.ZhuMianBan;

import java.awt.*;

import javax.swing.*;
import javax.swing.Timer;
import javax.swing.event.*;

import java.awt.event.*;
import java.util.ArrayList;

public class ZhangHaoMiMa extends JPanel implements ActionListener{
	/**
	 *
	 */
	private static final long serialVersionUID = -2736656843906930272L; //版本号
	private final int DELAY = 1000; //延时(毫秒)
	private JButton wangjimima,denglu,zhuce;
	private JLabel zhanghao,mima;
	private JTextField zhanghaokuang,mimakuang;
	private JLabel zhanghaotishikong,zhanghaotishicuo,mimatishikong,mimatishicuo;
	private JLabel dengluchenggong;
	private JComboBox xialakuang;
	private Timer zhanghaotimer,mimatimer;
	private String dengluzhanghao;
	private String denglumima;
	private String sqlyujv;
	private DAO dao;
	private Users use;

	public ZhangHaoMiMa(){
		//初始化各种控件
		zhanghao = new JLabel("账号:");
		mima = new JLabel("密码:");
		zhanghaokuang = new JTextField("请输入账号");
		xialakuang = new JComboBox();
		mimakuang = new JTextField("请输入密码");
		wangjimima = new JButton("忘记密码");
		denglu = new JButton("登录");
		zhuce = new JButton("注册");
		zhanghaotishikong = new JLabel("tips：账号为手机号或者身份证号");
		zhanghaotishicuo = new JLabel();
		mimatishikong = new JLabel();
		mimatishicuo = new JLabel();
		dengluchenggong = new JLabel();
		dao = new DAO();
		zhanghaotimer = new Timer(DELAY,this); //给账号提示计时器添加监听器(延时,监听器)
		mimatimer = new Timer(DELAY,this);
		wangjimima.addActionListener(this);//给忘记密码按钮添加监听器
		denglu.addActionListener(this);
		zhuce.addActionListener(this);

		zhanghaokuang.getDocument().addDocumentListener(new DocumentListener(){
			public void changedUpdate(DocumentEvent e){
			}
			public void removeUpdate(DocumentEvent e){
				// 当textField内容被删减时触发
				// 对应到用户行为上可以是用户按下退格键
			}
			public void insertUpdate(DocumentEvent e){
				// 当textField内容增加时触发
				// 对应到用户行为是用户键入内容
				dengluzhanghao = zhanghaokuang.getText();/*
		    	sqlyujv = "select * from users where zhanghao like '" + dengluzhanghao + "%'";
		    	ResultSet rs = dao.chaxun(sqlyujv);//发送当前输入的不完整账号与数据库比对
		    	ArrayList<String> list = new ArrayList();
		    	try {
					while(rs.next()){
						Users users = new Users();
						users.setZhanghao(rs.getString(1));
						users.setMima(rs.getString(2));
						list.add(users.getZhanghao());
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				int number = list.size();
				for(int i = 0; i < number; i++){
					xialakuang.addItem(list);
				}*/
			}
		});
		zhanghaokuang.addFocusListener(new FocusAdapter()//账号框的焦点监听
		{
			@Override
			public void focusGained(FocusEvent e)//获得焦点
			{
				if(zhanghaokuang.getText().equals("请输入账号") == true){
					zhanghaokuang.setText("");
				}
			}
			@Override
			public void focusLost(FocusEvent e)//失去焦点
			{
				if(zhanghaokuang.getText().equals("") == true){
					zhanghaokuang.setText("请输入账号");
				}
			}
		});
		mimakuang.addFocusListener(new FocusAdapter()//密码框的焦点监听
		{
			@Override
			public void focusGained(FocusEvent e)//获得焦点
			{
				if(mimakuang.getText().equals("请输入密码") == true){
					mimakuang.setText("");
				}
			}
			@Override
			public void focusLost(FocusEvent e)//失去焦点
			{
				if(mimakuang.getText().equals("") == true){
					mimakuang.setText("请输入密码");
				}
			}
		});

		this.setLayout(null);//不使用布局
		zhanghao.setBounds(40,20,100,30); //账号位置
		zhanghaokuang.setBounds(100, 20, 200, 30); //账号框位置
		zhanghaotishikong.setBounds(100,50,200,30); //账号为空提示框位置
		xialakuang.setBounds(100,50,200,100);
		mima.setBounds(40,80,100,30); //密码位置
		mimakuang.setBounds(100, 80, 200, 30); //密码框位置
		mimatishikong.setBounds(100,110,200,30); //密码为空提示框位置
		wangjimima.setBounds(340, 80, 100, 30); //忘记密码按钮位置
		denglu.setBounds(90,140,80,30); //登录按钮位置
		zhuce.setBounds(210,140,80,30); //注册按钮位置
		dengluchenggong.setBounds(90,170,280,30); //登录成功失败的提示框

		//向Panel添加控件
		add(zhanghao);
		add(zhanghaokuang);
		add(zhanghaotishikong);
		add(mima);
		add(mimakuang);
		add(mimatishikong);
		add(wangjimima);
		add(denglu);
		add(zhuce);
		add(dengluchenggong);
		//add(xialakuang);

		setBackground(Color.white);//背景颜色
		setPreferredSize(new Dimension(500,200)); //Panel长宽
	}

	public void actionPerformed(ActionEvent event){ //监听器
		if(event.getSource() == wangjimima){ //如果是忘记密码
			//弹框提示功能尚未完善
		}
		if(event.getSource() == denglu){
			if(zhanghaokuang.getText().equals("请输入账号")&&mimakuang.getText().equals("请输入密码")){ //如果账号或者密码为空
				zhanghaotishikong.setText("账号不能为空");//弹出提示账号密码不能为空
				mimatishikong.setText("密码不能为空");
			}
			else if(zhanghaokuang.getText().equals("请输入账号")){
				zhanghaotishikong.setText("账号不能为空");
			}
			else if(mimakuang.getText().equals("请输入密码")){
				mimatishikong.setText("密码不能为空");
			}
			else{
				dengluzhanghao = zhanghaokuang.getText();
				denglumima = mimakuang.getText();
				denglumima = denglumima + "1";
				sqlyujv = "select * from users where name = '" + dengluzhanghao + "'and pass = '" + denglumima + "'";
				boolean flag = dao.login(sqlyujv);//发送账号密码与数据库进行比对
				if(flag){ //如果账号密码正确
					ArrayList<Users> list = new ArrayList<Users>();
					sqlyujv = "select * from users where name = '" + dengluzhanghao + "'and pass = '" + denglumima + "'";
					list = dao.chaxun(sqlyujv);
					for(Users user : list){
						use = (Users) user;
					}
					dengluchenggong.setText("登录成功！点击进入主界面");
					denglu.setText("主界面");
					denglu.addActionListener(new ButtonListener());
					denglu.removeActionListener(this);
				}else{ //错误
					dengluchenggong.setText("登录失败！请检查输入的账号密码是否正确。");
					//弹出提示
				}
			}
		}
		if(event.getSource() == zhuce){ //如果是注册按钮
			JFrame frame = new JFrame ("注册界面");
			frame.setBackground(Color.white);
			frame.getContentPane().add(new ZhuCe());
			frame.setPreferredSize(new Dimension(600,500));
			frame.pack();
			frame.setVisible(true);
		}
		if(event.getSource() == zhanghaotimer){

		}
	}

	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event){
			JFrame frame = new JFrame ("主界面");
			frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
			frame.setBackground(Color.white);
			frame.getContentPane().add(new ZhuMianBan(use));
			frame.setPreferredSize(new Dimension(600,500));
			frame.pack();
			frame.setVisible(true);
		}
	}

	private class ButtonListener1 implements ActionListener
	{
		public void actionPerformed(ActionEvent event){
			JFrame frame = new JFrame ("注册界面");
			frame.setBackground(Color.white);
			frame.getContentPane().add(new ZhuCe());
			frame.setPreferredSize(new Dimension(600,300));
			frame.pack();
			frame.setVisible(true);
		}
	}
}
